"use strict";
$("body").ready(function() {
	var tid = 0;
	
	function updateImage() {
		chrome.runtime.sendMessage({name: "get-data-count"}, function(result) {
			$("#recyclebin").attr("src",(result.count > 0) ? "images/ico54-009.png" : "images/ico55-009.png");
		});	
	}
	
	function updateStatus() {
		chrome.runtime.sendMessage({name: "get-data-count"}, function(result) {
			$("#status").text("回收元素" + result.count + "个");
		});
	}
	
	$("#undo").click(function() {
		window.clearTimeout(tid);
		console.log("undo");
		chrome.runtime.sendMessage({name: "pop-item"}, function(result) {
			$("#status").text("已撤销移入(剩余元素" + result.count + "个)");
			updateImage();
			tid = window.setTimeout(updateStatus, 3000);
		});
	});
	
	$("#open_options").click(function() {
		console.log("open_options");
		chrome.runtime.openOptionsPage();
	});
	
	$("#clear_recyclebin").click(function() {
		console.log("clear_recyclebin");
		if (confirm("确定要清空回收站?")) {
			chrome.runtime.sendMessage({name: "clear-all-data"}, function(result) {
				updateImage();
				updateStatus();
			});
		}
	});
	
	updateImage();
	updateStatus();
});